//
//  UIBarButtonItem+Button.swift
//  LRWeibo
//
//  Created by 雷路荣 on 15/12/20.
//  Copyright © 2015年 leilurong. All rights reserved.
//

//import Foundation
import UIKit

extension UIBarButtonItem{
    /**
     便利构造函数,创建UIBarButtonIten
     
     - parameter imageName: 图片的正常状态的名称
     
     - returns: UIBarButtonItem
     */
    convenience init(imageName: String){// 在 extension 构造函数只能是 便利构造函数
//        let selectedImageName = imageName + "_highlighted"
        let button = UIButton()
        button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        button.sizeToFit()
        // 需要调用本类的指定构造函数
        self.init(customView: button)
    }
    /// 类方法创建UIBarButtonItem按钮(2张图片) 需要调用该方法创建 还不够方便
    func creatBarButtonItem(imageName:String) -> UIBarButtonItem {
        //选择状态下图片名
        let selectedImageName = imageName + "_highlighted"
        let button = UIButton()
        button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: selectedImageName), forState: UIControlState.Highlighted)
        button.sizeToFit()
        
        return UIBarButtonItem(customView: button)
    }
    
    
}